package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     *添加菜品
     * 改造添加菜品，加入清理缓存功能
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @CacheEvict(value = "dish",key = "#dishDTO.categoryId")//删除菜品对应的分类id的缓存
    public Result<String> add(@RequestBody DishDTO dishDTO) {
        dishService.add(dishDTO);
        //构造redis中的key，规则：dish_分类id
        String key = "dish_" + dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     *查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishVO> selectById(@PathVariable Integer id) {
        DishVO dishVO = dishService.selectById(id);
        return Result.success(dishVO);
    }


    /**
     *根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result<List<Dish>> selectListById(Long categoryId) {
        List<Dish> list = dishService.selectListById(categoryId);
        return Result.success(list);
    }


    /**
     *更新菜品
     * @param dishDTO
     * @return
     */
    @PutMapping()
    @CacheEvict(value = "dish",allEntries = true)//删除所有的缓存
    public Result<String> update(@RequestBody DishDTO dishDTO) {
        dishService.update(dishDTO);
        cleanCache("dish_*");
        return Result.success();
    }


    /**
     *分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     *更新状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "dish",key = "#id")//删除对应分类id的缓存
    public Result<String> startOrStop(@PathVariable Integer status ,Long id) {
        dishService.startOrStop(status,id);
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "dish",allEntries = true)//删除所有缓存
    public Result<String> delete(@RequestParam List<Long> ids) {
        dishService.delete(ids);
        cleanCache("dish_*");
        return Result.success();
    }


    /**
     * 只要执行增删改操作时，清理所有的缓存，先创建一个set集合，把通配符放入set集合
     * @param pattern
     */
    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
